<?php

use Workerman\Worker;

require_once __DIR__ . '/vendor/autoload.php';

// var_dump(ini('MQTT_HOST'));

$worker = new Worker();
$worker->onWorkerStart = function () {
    $mqtt = new Workerman\Mqtt\Client(
            ini('MQTT_HOST') . ':' . ini('MQTT_PORT'),
            [
                'username' => ini('MQTT_USERNAME'),
                'password' => ini('MQTT_PASSWORD'),
            ],
        );
    $mqtt->onConnect = function ($mqtt) {
        $data = [
            "CL" =>  0.5,
            "PH" => 7.7,
            "TDS" => 90,
            "WATER" => 100,
            "FILTER" => 300,
            "STATUS" => 1,
            "ALARM" =>  7,
            "GATEID" => 'gid001',
            // "EQPID" => 'sn001'
        ];
        $mqtt->publish('Topic_GO', json_encode($data));
    };


    // $mqtt->onConnect = function($mqtt) {
    //     $data = [
    //         "GATEID"=>"8200000024761CD6",
    //         "EQPID" => 'sn001',
    //         "CONTROL" => 1,
    //     ];

    //     $mqtt->publish('Topic_RETURN', json_encode($data));
    // };

    $mqtt->connect();
};
Worker::runAll();
